package com.it.commicity.controller.zy;


import com.alibaba.excel.EasyExcel;
import com.aliyun.oss.common.utils.StringUtils;
import com.it.commicity.common.Result;
import com.it.commicity.common.ResultCodeEnum;
import com.it.commicity.config.exception.ExcelException;
import com.it.commicity.domain.po.ZyRoom;
import com.it.commicity.domain.requestPass.Room;
import com.it.commicity.domain.vo.RoomVo;
import com.it.commicity.service.zy.IZyRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 房间  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/api/room")
public class ZyRoomController {
    @Autowired
    private IZyRoomService roomService;

    /**
     * 批删+单删
     *
     * @return
     */
    @PostMapping("/delZyRoom")
    public Result deleteByRoomIds(@RequestBody ArrayList<Long> roomIds) {
        return roomService.deleteByRoomIds(roomIds);
    }

    /**
     * 添加修改+校验
     *
     * @param zyRoom
     * @return
     */
    @PostMapping("/updateAndSaveZyRoom")
    public Result updateAndSaveZyRoom(@RequestBody ZyRoom zyRoom) {
        System.err.println("zy-ZyRoomController-updateAndSaveZyRoom->" + zyRoom);
        return roomService.updateAndSaveZyRoom(zyRoom);
    }

    /**
     * 分页查询所有数据
     *
     * @param room
     * @return
     */
    @GetMapping("/getZyRoomList")
    public Result getZyRoomList(Room room) {
//        System.err.println("room=" + room.getCommunityId());
        return roomService.getRoomPage(room);
    }

    //    zyRoom/getZyBuildingAndUnit
    @GetMapping("/getZyBuildingAndUnit")
    public Result getZyBuildingAndUnit(String communityId) {
        if (StringUtils.isNullOrEmpty(communityId)) {
            return Result.fail("楼栋单元获取失败");
        }
        return roomService.getZyBuildingAndUnit(communityId);
    }

    @PostMapping("/getExcel")
    public void getExcel(@RequestBody ArrayList<Long> roomIds,
                         @RequestParam Long communityId, HttpServletResponse response) {
        try {
            // 1. 查询数据（根据IDs获取要导出的数据列表）
            List<RoomVo> dataList = roomService.getExportData(roomIds, communityId);
            // 2. 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("房屋信息.xls", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            // 3. 使用EasyExcel导出
            EasyExcel.write(response.getOutputStream(), RoomVo.class)
                    .sheet("房屋数据")
                    .doWrite(dataList);
        } catch (Exception e) {
            System.err.println(e);
            throw new ExcelException(ResultCodeEnum.EXCEL_EXPORT_ERROR);
        }
    }
}
